import '../public/index'
import url from '../data/url'
import request from '../data/request'
import { tableConfig } from '../data/config'
import { getUrlParam } from '../data/util'
import store from 'store'

const base = url.base,
  getBuildConvert  = request. getBuildConvert;

let table,
    storeData,
    id = getUrlParam('id')

// 初始化表格
table = $('#table').bootstrapTable($.extend({}, tableConfig, {
  url: base +  getBuildConvert ,
  queryParams: function (params) {
    return {
        currentPage: params.pageNumber,
        pageSize: params.pageSize,
        id: id
      }
  },
  columns: [
    {
      field: 'idStr',
      title: '编号',
      valign: 'middle'
    }, {
      field: 'buildingId',
      title: '楼盘Id',
      valign: 'middle'
    }, {
      field: 'channel',
      title: '渠道名称',
      valign: 'middle'
    }, {
      field: 'convertId',
      title: '转换Id',
      valign: 'middle'
    }, {
      field: 'convertName',
      title: '转换名称',
      valign: 'middle'
    }, {
      field: 'action',
      valign: 'middle',
      align: 'center',
      title: '操作',
      formatter: function (index, row) {
        return `<div class="btn-group">            
                        <button data-id="${ row.idStr }" type="button" class="btn btn-modify btn-default">
                          修改
                        </button>    
                    </div>`
      }
    }]
}))

// 点击列表的时候传递数据到全局变量
table.on('click-row.bs.table', (e, data, el) => {
  storeData = data
})

// 搜索
$('#search-btn').on('click', (e) => {
  e.preventDefault();

  table.bootstrapTable('refresh', {
    query: $.extend({
      currentPage : 1,
    }, $('#searchForm').serializeObject())
  })
})

//跳转至修改页
$(document).on('click', '#table .btn-modify', (e) => {
  e.preventDefault()

  console.log('修改')
  store.set('storeData', storeData)

  window.location.href = `./convertModify.html?id=${ id }`
})

$('#convert').on('click', (e) => {
  e.preventDefault;

  window.location.href = `./convertAdd.html?id=${ id }`
})